package main

import (
	"fmt"
	"sort"
)

func search2(nums []int, target int) int {
	if len(nums) <= 3 {
		for k, v := range nums {
			if v == target {
				return k
			}
		}
		return -1
	}
	nums1 := make([]int, len(nums))
	copy(nums1, nums) //nums1  正序
	sort.Ints(nums1)
	zx := search1(nums1, target)
	if zx == -1 {
		return -1
	}
	dd := search1(nums1, nums[0])
	if nums[0] <= target {
		return zx - dd
	}
	return len(nums) - dd + zx
}
func search1(nums []int, target int) int {
	l, r := 0, len(nums)-1
	if r == -1 {
		return -1
	}
	for l <= r {
		m := (r-l)/2 + l
		if nums[m] == target {
			return m
		} else if nums[m] > target {
			r = m - 1
		} else if nums[m] < target {
			l = m + 1
		}
	}
	return -1

}

func main() {
	//fmt.Println("0:   ", search2([]int{4, 5, 6, 7, 0, 1, 2}, 4))
	fmt.Println("3:  ", search2([]int{7, 8, 1, 2, 3, 4, 5, 6}, 2))
	//fmt.Println("1:  ", search2([]int{1, 3}, 3))
}
